package org.paul.gotobplan.ui;

import android.util.Log;
import android.widget.TextView;
import org.androidannotations.annotations.*;
import org.paul.gotobplan.BaseAct;
import org.paul.gotobplan.R;
import org.paul.gotobplan.bean.SaveBean;
import org.paul.gotobplan.bean.TokenBean;
import org.paul.gotobplan.bean.UserBean;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

@EActivity(R.layout.act_signout)
public class SignOutAct extends BaseAct {

    private static final int LOCATION_REQUEST = 0x11;

    @ViewById(R.id.tv_token)
    TextView tvToken;
    @ViewById(R.id.tv_user)
    TextView tvUser;
    @ViewById(R.id.tv_am)
    TextView tvAm;
    @ViewById(R.id.tv_pm)
    TextView tvPm;

    @Click(R.id.btn_token)
    void token() {
        getToken();
    }

    @Click(R.id.btn_location)
    void location() {
        LocationAct_.intent(this).startForResult(LOCATION_REQUEST);
    }

    @OnActivityResult(LOCATION_REQUEST)
    void onResult(@OnActivityResult.Extra String Longitude, @OnActivityResult.Extra String Latitude) {

    }

    @Background
    void getToken() {
//        String token = restManager.token();
//        token = token.substring(1, token.length() - 1);
//        token = token.replace("\\", "");
//        try {
//            JSONObject jsonObject = new JSONObject(token);
//            String access_token = jsonObject.getString("access_token");
//            this.token = access_token;
//            Log.d("paul", access_token);
//            tokenSuccess(token);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
        TokenBean tokenBean = restManager.token();
        String access_token = tokenBean.getAccess_token();
        this.token = access_token;
        tokenSuccess(access_token);
    }

    @UiThread
    void tokenSuccess(String token) {
        tvToken.setText(token);
    }

    private String token;

    @Click(R.id.btn_user)
    void user() {
        getUserToken();
    }

    private UserBean userBean;

    @Background
    void getUserToken() {
        Map<String, Object> params = new HashMap<>();
        params.put("UserName", "d1QFZVhuoeo=");
//        String t="Bearer "+token;
        userBean = restManager.user(token, params);
//        try {
//            JSONObject jsonObject = new JSONObject(user);
//            String token = jsonObject.getString("token");
//            Log.d("paul",token);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
        userBeanSuccess();
        Log.d("paul", userBean.toString());
    }

    @UiThread
    void userBeanSuccess() {
        tvUser.setText(userBean.toString());
    }

    @Background
    void location(int status) {
        Map<String, Object> params = new HashMap<>();
        double minW = 34.263092;
        double maxW = 34.263142;
        double minJ = 108.773782;
        double maxJ = 108.773962;
//        for(int i=0;i<100000;i++) {
            double j = randomDouble(minJ, maxJ, 1);
            double w = randomDouble(minW, maxW, 1);
            Log.d(TAG, "j=" + j + "w=" + w);
//        }
        params.put("attencePointAddr", "西安市长安区");
        params.put("attencePointLongitude", /*status == 0 ? 108.773782 : 108.773962*/
                j);
        params.put("attencePointLatitude", /*status == 0 ? 34.263092 : 34.263142*/
                w);
        params.put("attencePointName", "沣东场地 中俄丝路创新园-东门");
        params.put("attenceDeviceID", "IMEI_99001203279021_PUID_356616595626247_sim_89860315249101448441");
        params.put("attenceType", 1);
        params.put("attenceRemark", status == 0 ? "签到" : "签退");
        params.put("token", userBean.getToken());

        SaveBean saveBean = status == 0 ? restManager.am(token, params) : restManager.pm(token, params);
        Log.d("paul", saveBean.toString());
        saveSuccess(saveBean, status);
    }

    private static double randomDouble(double start,double end,int decimal){
        DecimalFormat df=new DecimalFormat("###.000000");
        double rtnn = start + Math.random() * (end - start);
        if (rtnn == start || rtnn == end) {
            return randomDouble(start, end,decimal);
        }
        String ret = df.format(rtnn);
        System.out.println("ret="+ret);
        return Double.valueOf(ret);
    }

    @UiThread
    void saveSuccess(SaveBean saveBean, int status) {
        if (status == 0) {
            tvAm.setText(saveBean.toString());
        } else {
            tvPm.setText(saveBean.toString());
        }
    }

    @Click(R.id.btn_am)
    void am() {
//        location(0);
    }

    @LongClick(R.id.btn_am)
    void longAm() {
        location(0);
    }

    @Click(R.id.btn_pm)
    void pm() {
        location(1);
    }

    @UiThread
    void showRet(TokenBean bean) {
        dismissProgress();
        tvToken.setText(bean.toString());
    }

}
